using AutoMapper;
using Enter.Dashboard.Api.Dtoes.Requests;
using Enter.Dashboard.Api.Entities;
using Enter.Dashboard.Api.Filters;
using Enter.Enums;
using Enter.Utils;
using Enter.Utils.CryptoExt;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Caching.Distributed;
using Microsoft.Extensions.Logging;
using NSwag.CodeGeneration.TypeScript;
using NSwag;
using System.Threading.Tasks;
using NJsonSchema.CodeGeneration.TypeScript;
namespace Enter.Dashboard.Api.Controllers
{
    [ApiController]
    [Route("[controller]/[action]")]
    [AuthorizationAttribute(NeedLogin = false)]
    public class ScriptController : BaseController
    {
        //private readonly IMapper _mapper;

        public ScriptController(ILogger<LoginController> logger, IDistributedCache cache, IFreeSql fsql, IMapper mapper) : base(logger, cache, fsql, mapper)
        {
            //_mapper = mapper;
        }
        // Script/TypeScript
        public async Task<string> TypeScript()
        {
            string baseUrl = $"{this.Request.Scheme}://{this.Request.Host.Value.ToString()}{this.Request.PathBase.Value.ToString()}";
            OpenApiDocument document = await OpenApiDocument.FromUrlAsync($"{baseUrl}/swagger/v1/swagger.json");
            //var document = await OpenApiDocument.FromUrlAsync("swaggerSpecUrl");            
            var settings = new TypeScriptClientGeneratorSettings
            {
                // https://github.com/RicoSuter/NSwag/wiki/TypeScriptClientGenerator
                // https://github.com/RicoSuter/NJsonSchema/wiki/TypeScriptGeneratorSettings
                TypeScriptGeneratorSettings = {
                    //Namespace = "ServiceAPI" ,
                    TypeScriptVersion = 3.9M,
                    TypeStyle = TypeScriptTypeStyle.Interface,// Interface,Class or KoObservableClass
                    DateTimeType = TypeScriptDateTimeType.String,//Date, MomentJS or String
                },                
                //ClientBaseClass = "AuthorizedApiBase", // export class ClassName extends ClientBaseClass
                Template = TypeScriptTemplate.Axios,
                PromiseType = PromiseType.Promise,
                HttpClass = HttpClass.HttpClient,
                //InjectionTokenType = InjectionTokenType.InjectionToken,
                //ConfigurationClass = "ApiClientConfig",
                ClassName = "{controller}Client",
            };

            var generator = new TypeScriptClientGenerator(document, settings);
            var code = generator.GenerateFile();
            return code;
        }
    }
}
